/*
 * gets_s.c - Safe input function with bounds checking
 */

#include "bounds_check.h"
#include "bounds_check_internal.h"
#include <stdio.h>
#include <string.h>

int gets_s(char *buffer, size_t buffer_size) {
    if (!is_valid_buffer(buffer, buffer_size)) {
        return BOUNDS_CHECK_INVALID_PARAM;
    }

    if (fgets(buffer, (int)buffer_size, stdin) == NULL) {
        return BOUNDS_CHECK_FAILURE;
    }

    /* Remove newline character if present */
    size_t len = 0;
    strnlen_s(buffer, buffer_size, &len);
    if (len > 0 && buffer[len - 1] == '\n') {
        buffer[len - 1] = '\0';
    }

    return BOUNDS_CHECK_SUCCESS;
}